package com.gr.blog.mean.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gr.blog.mean.dao.MeanDao;
import com.gr.blog.mean.model.MenuModel;
import com.gr.blog.mean.service.MeanService;
import com.gr.blog.utils.CollectionsUtil;
import com.gr.blog.utils.CommonUtils;
import com.gr.blog.utils.Constants;
import com.gr.blog.utils.PageHelperUtils;

@Service("meanService")
public class MeanServiceImpl implements MeanService {

	@Autowired
	private MeanDao meanDao;
	
	/**
	 * 得到所有的菜单数据
	 */
	@Override
	public List<Map<String,Object>> showAllTree(Map<String,Object> menuMap) {
		return meanDao.selectAllMean(menuMap);
	}
	/**
	 * 超级管理园查询所有的菜单
	 */
	@Override
	public List<Map<String, Object>> showSuperAllTree(Map<String, Object> menuMap) {
		return meanDao.selectSuperAllTree(menuMap);
	}
	/**
	 * 分页查询显示需要的数据
	 */
	@Override
	public PageHelperUtils<MenuModel> showALLTreeMenu(String menuName, int page, int start,
			int limit) {
		Example example = new Example(MenuModel.class);
		PageHelper.startPage((page-1)*limit, limit);
		if (CommonUtils.isNotEmpty(menuName)) {
			example.createCriteria().andLike("menuName", "%"+menuName+"%");
		}
		example.createCriteria().andEqualTo("isEnabled", "0");
		example.setOrderByClause("show_order DESC");
		List<MenuModel> deptList = meanDao.selectByExample(example);
		PageInfo<MenuModel> pageInfo = new PageInfo<>(deptList);
		return new PageHelperUtils(deptList,(int)pageInfo.getTotal());
	}
	@Override
	public List<Map<String,Object>> findFeatherMenu() {
		return meanDao.selectFeatherMenu();
	}
	@Override
	public int addMenuInfoToDb(Map<String, Object> menuMap) {
		String fatherId = CollectionsUtil.getStringByMap(menuMap, "fatherId");
		String url = CollectionsUtil.getStringByMap(menuMap, "url");
		Map<String,Object> map = new HashMap<>();
		if (CommonUtils.isNotEmpty(fatherId) && CommonUtils.isNotEmpty(url)) {//!= null 表示是子节点，的计算子节点的menuCode
			int fatherLength = fatherId.length();
			map.put("mCode", fatherId);
			map.put("mcodelength", fatherLength*2);
			menuMap.put("childNode","1");//插入时候用
		}else if (CommonUtils.isNotEmpty(fatherId)){
			int fatherLength = fatherId.length();
			map.put("mCode", fatherId);
			map.put("mcodelength", fatherLength*2);
			menuMap.put("childNode","0");//插入时候用
		} else {// == null 表示是顶层节点， 的计算父节点的menuCode
			map.put("node","root");
			menuMap.put("url","");
			menuMap.put("childNode","0");
		}
		String maxMenuCode = meanDao.selectMaxMenuCode(map);
		int mcLength = maxMenuCode.length();
		int maxCode = Integer.valueOf(maxMenuCode);
		String resMaxCode = "";
		if (3 == mcLength && maxCode >0 && maxCode <10) {
			resMaxCode ="00"+(maxCode +1);
		} else if (3 == mcLength && maxCode >=10){
			resMaxCode ="0"+(maxCode +1);
		} else if (6 == mcLength && maxCode >1000 && maxCode < 9999) {
			resMaxCode ="00"+(maxCode +1);
		} else if (6 == mcLength && maxCode >= 9999){
			resMaxCode ="0"+(maxCode +1);
		} else if (9 == mcLength && maxCode >1000000 && maxCode < 9999999) {
			resMaxCode ="00"+(maxCode +1);
		} else if (9 == mcLength && maxCode >= 9999999){
			resMaxCode ="0"+(maxCode +1);
		}
		System.out.println("maxCode:"+resMaxCode);
		menuMap.put("mid", CommonUtils.get4bitRandom());
		menuMap.put("menuCode", resMaxCode);
		menuMap.put("showOrder", getMaxShowOrder()+1);
		menuMap.put("isEnabled", Constants.C_TYPE_0);// 默认为0
		menuMap.put("operateTime", CommonUtils.getStringCurrentTime());
		int insertNum = meanDao.insertMenuInfoToDB(menuMap);
		return insertNum;
	}

	@Override
	public List<Map<String,Object>> findMenuAndDept() {
		return meanDao.selectMenuAndDept();
	}
	@Override
	public List<MenuModel> showLeftManuDate(Map<String, Object> deptMap) {
		return meanDao.selectShowLeftManuDate(deptMap);
	}
	@Override
	public List<MenuModel> showRightManuDate(Map<String, Object> deptMap) {
		return meanDao.selectShowRightManuDate(deptMap);
	}
	
	@Override
	public List<MenuModel> findMenuDateByUserName(String username) {
		return meanDao.selectMenuDateByUserName(username);
	}
	@Override
	public List<Integer> findMenuAndDeptLinkByMenuId(String menuId) {
		return meanDao.selectMenuAndDeptLinkByMenuId(menuId);
	}
	@Override
	public int deleteMenuInfoByMenuId(String menuId) {
		return meanDao.deleteMenuInfoByMenuId(menuId);
	}

	//查询最大的菜单顺序
	public int getMaxShowOrder(){
		return meanDao.selectMaxShowOrder();
	}
	
}
